package dal.datastore;

import java.util.ArrayList;
import java.util.Iterator;

public class DatabaseData {
	private String dbname;
	private ArrayList<TableData> tables;

	public DatabaseData(String dbname) {
		this.dbname = dbname;
		this.tables = new ArrayList<TableData>();
	}

	public DatabaseData(String dbname, ArrayList<TableData> data) {
		this.tables = data;
		this.dbname = dbname;
	}

	public ArrayList<TableData> getTableData() {
		return this.tables;
	}

	public String getDbName() {
		return this.dbname;
	}

	public void setDbName(String name) {
		this.dbname = name;
	}

	public void setTablesData(ArrayList<TableData> data) {
		this.tables = data;
	}

	/**
	 * Get table data
	 * 
	 * @param tableName
	 * @return
	 */
	public TableData getTableData(String tableName) {
		for (int i = 0; i < this.tables.size(); i++) {
			if (this.tables.get(i).getTableName()
					.compareToIgnoreCase(tableName) == 0) {
				return this.tables.get(i);
			}
		}
		return null;
	}

	public void setTableData(String tableName, TableData newTable) {
		for (int i = 0; i < this.tables.size(); i++) {
			if (this.tables.get(i).getTableName()
					.compareToIgnoreCase(tableName) == 0) {
				this.tables.set(i, newTable);
			}
		}
	}

	/**
	 * Remove table data from tables
	 * 
	 * @param table
	 * @return
	 * @author Stanley
	 */
	public boolean removeTableData(TableData table) {
		Iterator<TableData> i = tables.iterator();
		while (i.hasNext()) {
			TableData tableData = (TableData) i.next();
			if (tableData.equals(table)) {
				tables.remove(table);
				return true;
			}
		}
		return false;
	}

	/**
	 * Add table data to tables
	 * 
	 * @param table
	 * @return
	 * @author Stanley
	 */
	public void addTableData(TableData table) {
		tables.add(table);
	}
}
